package com.example.controller;

import com.example.entity.Staffs;
import com.example.entity.Village;
import com.example.result.CommonResult;
import com.example.service.VillageSerivce;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @project: smart-property
 * @description: 小区管理 controller 层
 * @author: cjq
 * @create: 2022-11-15 10:51
 **/
@RestController
@RequestMapping("/village")
public class VillageController {
    @Autowired
    VillageSerivce villageSerivce;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 查询小区编号是否有重复
     * @param villageNumber
     * @return
     */
    @GetMapping("/listVillageNumber")
    public CommonResult listVillageNumber(String villageNumber){
        return CommonResult.ok(villageSerivce.listVillageNumber(villageNumber));
    }

    /**
     * 添加/修改小区信息
     * @param file
     * @param village
     * @param staffs
     * @return
     */
    @PostMapping("/saveVillageInfo")
    public CommonResult saveVillageInfo(MultipartFile[] file, Village village, Staffs staffs){
        return CommonResult.ok(villageSerivce.saveVillageInfo(file,village,staffs));
    }

    /**
     * 小区信息列表展示
     * @return
     */
    @GetMapping("/queryVillageInfo")
    public CommonResult queryVillageInfo(){
        List<Village> list = (List<Village>) redisUtil.get("villageInfo");
        if (list == null){
            list = villageSerivce.queryVillageInfo();
            redisUtil.set("villageInfo",list);
        }
        return CommonResult.ok(list);
    }

    /**
     * 删除小区信息
     * @param id
     * @return
     */
    @DeleteMapping("/delVillageInfo")
    public CommonResult delVillageInfo(Integer id){
        return CommonResult.ok(villageSerivce.delVillageInfo(id));
    }
}
